package com.zx.BookSeaReader.greendao.gen;

import java.util.List;
import android.database.Cursor;
import android.database.sqlite.SQLiteStatement;

import org.greenrobot.greendao.AbstractDao;
import org.greenrobot.greendao.Property;
import org.greenrobot.greendao.internal.DaoConfig;
import org.greenrobot.greendao.database.Database;
import org.greenrobot.greendao.database.DatabaseStatement;
import org.greenrobot.greendao.query.Query;
import org.greenrobot.greendao.query.QueryBuilder;

import com.zx.BookSeaReader.greendao.entity.BookListSubscribe;
import com.zx.BookSeaReader.greendao.entity.SubscribeBean;

import com.zx.BookSeaReader.greendao.entity.UserBean;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/** 
 * DAO for table "USER_BEAN".
*/
public class UserBeanDao extends AbstractDao<UserBean, Long> {

    public static final String TABLENAME = "USER_BEAN";

    /**
     * Properties of entity UserBean.<br/>
     * Can be used for QueryBuilder and for referencing column names.
     */
    public static class Properties {
        public final static Property Userid = new Property(0, Long.class, "userid", true, "_id");
        public final static Property Username = new Property(1, String.class, "username", false, "USERNAME");
        public final static Property Password = new Property(2, String.class, "password", false, "PASSWORD");
        public final static Property Admin = new Property(3, int.class, "admin", false, "ADMIN");
        public final static Property Gender = new Property(4, String.class, "gender", false, "GENDER");
    }

    private DaoSession daoSession;

    private Query<UserBean> book_UsersQuery;
    private Query<UserBean> bookListBean_UserBeansQuery;

    public UserBeanDao(DaoConfig config) {
        super(config);
    }
    
    public UserBeanDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
        this.daoSession = daoSession;
    }

    /** Creates the underlying database table. */
    public static void createTable(Database db, boolean ifNotExists) {
        String constraint = ifNotExists? "IF NOT EXISTS ": "";
        db.execSQL("CREATE TABLE " + constraint + "\"USER_BEAN\" (" + //
                "\"_id\" INTEGER PRIMARY KEY AUTOINCREMENT ," + // 0: userid
                "\"USERNAME\" TEXT NOT NULL ," + // 1: username
                "\"PASSWORD\" TEXT NOT NULL ," + // 2: password
                "\"ADMIN\" INTEGER NOT NULL ," + // 3: admin
                "\"GENDER\" TEXT);"); // 4: gender
    }

    /** Drops the underlying database table. */
    public static void dropTable(Database db, boolean ifExists) {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"USER_BEAN\"";
        db.execSQL(sql);
    }

    @Override
    protected final void bindValues(DatabaseStatement stmt, UserBean entity) {
        stmt.clearBindings();
 
        Long userid = entity.getUserid();
        if (userid != null) {
            stmt.bindLong(1, userid);
        }
        stmt.bindString(2, entity.getUsername());
        stmt.bindString(3, entity.getPassword());
        stmt.bindLong(4, entity.getAdmin());
 
        String gender = entity.getGender();
        if (gender != null) {
            stmt.bindString(5, gender);
        }
    }

    @Override
    protected final void bindValues(SQLiteStatement stmt, UserBean entity) {
        stmt.clearBindings();
 
        Long userid = entity.getUserid();
        if (userid != null) {
            stmt.bindLong(1, userid);
        }
        stmt.bindString(2, entity.getUsername());
        stmt.bindString(3, entity.getPassword());
        stmt.bindLong(4, entity.getAdmin());
 
        String gender = entity.getGender();
        if (gender != null) {
            stmt.bindString(5, gender);
        }
    }

    @Override
    protected final void attachEntity(UserBean entity) {
        super.attachEntity(entity);
        entity.__setDaoSession(daoSession);
    }

    @Override
    public Long readKey(Cursor cursor, int offset) {
        return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
    }    

    @Override
    public UserBean readEntity(Cursor cursor, int offset) {
        UserBean entity = new UserBean( //
            cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // userid
            cursor.getString(offset + 1), // username
            cursor.getString(offset + 2), // password
            cursor.getInt(offset + 3), // admin
            cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4) // gender
        );
        return entity;
    }
     
    @Override
    public void readEntity(Cursor cursor, UserBean entity, int offset) {
        entity.setUserid(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
        entity.setUsername(cursor.getString(offset + 1));
        entity.setPassword(cursor.getString(offset + 2));
        entity.setAdmin(cursor.getInt(offset + 3));
        entity.setGender(cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4));
     }
    
    @Override
    protected final Long updateKeyAfterInsert(UserBean entity, long rowId) {
        entity.setUserid(rowId);
        return rowId;
    }
    
    @Override
    public Long getKey(UserBean entity) {
        if(entity != null) {
            return entity.getUserid();
        } else {
            return null;
        }
    }

    @Override
    public boolean hasKey(UserBean entity) {
        return entity.getUserid() != null;
    }

    @Override
    protected final boolean isEntityUpdateable() {
        return true;
    }
    
    /** Internal query to resolve the "users" to-many relationship of Book. */
    public List<UserBean> _queryBook_Users(String bookid) {
        synchronized (this) {
            if (book_UsersQuery == null) {
                QueryBuilder<UserBean> queryBuilder = queryBuilder();
                queryBuilder.join(SubscribeBean.class, SubscribeBeanDao.Properties.Userid)
                    .where(SubscribeBeanDao.Properties.Bookid.eq(bookid));
                book_UsersQuery = queryBuilder.build();
            }
        }
        Query<UserBean> query = book_UsersQuery.forCurrentThread();
        query.setParameter(0, bookid);
        return query.list();
    }

    /** Internal query to resolve the "userBeans" to-many relationship of BookListBean. */
    public List<UserBean> _queryBookListBean_UserBeans(Long bookListid) {
        synchronized (this) {
            if (bookListBean_UserBeansQuery == null) {
                QueryBuilder<UserBean> queryBuilder = queryBuilder();
                queryBuilder.join(BookListSubscribe.class, BookListSubscribeDao.Properties.Userid)
                    .where(BookListSubscribeDao.Properties.BookListid.eq(bookListid));
                bookListBean_UserBeansQuery = queryBuilder.build();
            }
        }
        Query<UserBean> query = bookListBean_UserBeansQuery.forCurrentThread();
        query.setParameter(0, bookListid);
        return query.list();
    }

}
